package com.css.fxfzxczh.modules.resultsManagement.repository.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.resultsManagement.entity.MapManagementEntity;
import com.css.fxfzxczh.modules.resultsManagement.repository.MapManagementRepository;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class MapManagementRepositoryImpl implements MapManagementRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void saveMap(MapManagementEntity mapManagementEntity) {
        String sql = "insert into map_management (map_id,xsem_id,attach_id,xsem_name,map_name,map_description," +
                "map_label,uploader,upload_time,del_flag,create_user,create_time,unit) values " +
                "(:mapId,:xsemId,:attachId,:xsemName,:mapName,:mapDescription,:mapLabel," +
                ":uploader,:uploadTime,:delFlag,:createUser,:createTime,:unit)";
        BeanPropertySqlParameterSource source = new BeanPropertySqlParameterSource(mapManagementEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public MapManagementEntity getById(String id) {
        String sql = "select * from map_management where map_id='" + id + "' and del_flag ='" + YNEnum.N.toString() + "'";
        MapManagementEntity mapManagementEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MapManagementEntity.class));
        return mapManagementEntity;
    }

    @Override
    public void deleteByIds(List<String> ids, Timestamp timestamp) {
        String sql = "update map_management set del_flag = '" + YNEnum.Y + "' , update_time = '" + timestamp + "' " + " where map_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);

    }

    @Override
    public MapManagementEntity update(MapManagementEntity mapManagementEntity) {
        String sql = "update map_management set xsem_id=:xsemId,attach_id=:attachId,xsem_name=:xsemName,map_name=:mapName,map_label=:mapLabel," +
                "map_description=:mapDescription,uploader=:uploader," +
                "upload_time=:uploadTime,del_flag=:delFlag,update_user=:updateUser,update_time=:updateTime,unit=:unit where map_id=:mapId";
        SqlParameterSource source = new BeanPropertySqlParameterSource(mapManagementEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return mapManagementEntity;
    }

    @Override
    public MapManagementEntity getByLabel(String xsemId, String mapLabel) {
        try {
            String sql = "select * from map_management where xsem_id='" + xsemId + "' and del_flag ='" + YNEnum.N.toString() + "' and map_label ='" + mapLabel + "'";
            MapManagementEntity mapManagementEntitity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(MapManagementEntity.class));
            return mapManagementEntitity;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<MapManagementEntity> getByLabel(String xsemId) {
        try {
            String sql = "select * from map_management where xsem_id='" + xsemId + "' and del_flag ='" + YNEnum.N.toString() + "'";
            return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(MapManagementEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }
}
